<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.codeName" placeholder="业务编码名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="filters.productCode" placeholder="业务编码"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="downfindPageCode">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary"  @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="UpProductCodes" highlight-current-row v-loading="listLoading" border  max-height="620"  style="width: 100%;">
			<el-table-column type="index" width="80" label="编号">
			</el-table-column>
			<el-table-column prop="codeName" label="编码名称"   >
			</el-table-column>
			<el-table-column prop="productCode" label="业务编码" >
			</el-table-column>
			<el-table-column prop="createTime" label="创建时间" min-width="100"    >
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<!--<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>-->
				</template>
			</el-table-column>
		</el-table>

		<el-pagination
				@current-change="handleCurrentChange"
				@size-change="handleSizeChange"
				layout="total,sizes,prev, pager, next"
				:total="total">
		</el-pagination>

		<el-dialog title="编辑业务编码" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="名称" prop="codeName">
					<el-input v-model="editForm.codeName" auto-complete="off"></el-input>
				</el-form-item>

				<el-form-item label="代码" prop="productCode">
					<el-input v-model="editForm.productCode" ></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<el-dialog title="新增业务编码" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="名称" prop="codeName">
					<el-input v-model="addForm.codeName"  ></el-input>
				</el-form-item>
				<el-form-item label="代码" prop="productCode">
					<el-input v-model="addForm.productCode"  ></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
    import util from '../../common/js/util'
    //import NProgress from 'nprogress'
    import { downfindPageCode,downaddProductCode,downupdateProductCode} from '../../api/api';

    export default {
        data() {
            return {
                filters: {
                    codeName: '',
                    productCode:''
                },
                UpProductCodes: [],
                total: 0,
                start:0,
                pageSize:10,
                listLoading: false,
                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {
                    codeName: [
                        { required: true, message: '请输入业务编码名称', trigger: 'blur' },
                        { min: 1, max: 30, message: '长度在 1 到 20 个字符', trigger: 'blur' }
                    ],
                    productCode:[
                        { min: 1, max: 30, message: '长度在 1 到 20 个字符', trigger: 'blur' },
                        { required: true, message: '请输入业务编码', trigger: 'blur' }
                    ]
                },
                //新增界面数据
                addForm: {
                    codeName: '',
                    productCode: ''
                },
                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    codeName: [
                        { required: true, message: '请输入业务编码名称', trigger: 'blur' },
                        { min: 1, max: 30, message: '长度在 1 到 20 个字符', trigger: 'blur' }
                    ],
                    productCode:[
                        { min: 1, max: 30, message: '长度在 1 到 20 个字符', trigger: 'blur' },
                        { required: true, message: '请输入业务编码', trigger: 'blur' }
                    ]
                },
                //编辑界面数据
                editForm: {
                    codeName: '',
                    productCode: ''
                },
            }
        },
        methods: {
            //获取用户列表
            downfindPageCode() {
                let para = {
                    start: this.start,
                    pageSize:this.pageSize,
                    codeName: this.filters.codeName,
                    productCode:this.filters.productCode
                };
                this.listLoading = true;
                downfindPageCode(para).then((datas) => {
                    this.listLoading = false;
                    let { message, code, data } = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.UpProductCodes = data.result;
                        this.total = data.totalCount;
                    }
                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            let para = Object.assign({}, this.editForm);
                            downupdateProductCode(para).then((datas) => {
                                this.editLoading = false;
                                let { message, code, data } = datas;
                                if (code !== "0") {
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }

                                this.$refs['editForm'].resetFields();
                                this.editFormVisible = false;
                                this.downfindPageCode();
                            });
                        });
                    }
                });
            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    busname: '',
                    busCode: '',
                    remark: ''
                };
            },//新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            let para = Object.assign({}, this.addForm);
                            downaddProductCode(para).then((datas) => {
                                this.addLoading = false;
                                let { message, code, data } = datas;
                                if (code !== "0") {
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                this.downfindPageCode();
                            }).catch(function (error) {
                                console.log(error);
                                this.addLoading = false;
                            });

                        });
                    }
                });
            }

            ,handleCurrentChange:function(val){
                this.start = val - 1;
                this.downfindPageCode();
            },
            handleSizeChange:function(val){
                this.pageSize = val;
                this.downfindPageCode();
            }
        },
        mounted() {
            this.downfindPageCode();
        }
    }

</script>

<style scoped>

</style>
